<template>
    <div class="favorites">
        <TopNav></TopNav>
        <div class="el-body-box">
            <el-main class="elMain">
                <div class="show-line">
                    <div class="line"></div>
                    <div class="label">
                        全部商品
                        <span class="amount">({{collectList.length}})</span>
                        <span class="manager-more" @click="showDelete=!showDelete">{{showDelete?'退出批量管理':'批量管理'}}</span>
                        <span style="margin: 0px 50px" v-if="showDelete">
                            <el-checkbox v-model="chooseAll" @change="chooseAllChange()">全选</el-checkbox>
                        </span>
                        <span v-if="showDelete">
                            <el-button type="text" size="mini" style="color: red" @click="deleteMore()">删除</el-button>
                        </span>
                    </div>
                </div>

                <div style="width: 100%;">
                    <el-row :gutter="20" style="width: 100%;">
                        <el-col :span="6" v-for="(item,index) in collectList" :key="index" style="margin: 10px 0px">
                            <div class="commodity">
                                <span style="position: absolute;left: 10px;top: 5px;" v-if="showDelete">
                                    <el-checkbox v-model="item.radio"></el-checkbox>
                                </span>
                                <div><img :src="item.commodityImgurl?imgUrl+item.commodityImgurl:src" class="img"></div>
                                <div
                                        style="text-align: right;position: relative;bottom: 50px;right: 20px;cursor: pointer;font-size: 24px;color: #FFFFFF">
                                    <i class="el-icon-delete" @click="deleteOne(item)"></i></div>
                                <div class="info">
                                    <div>
                                        <div class="name">{{item.commodityName}}</div>
                                        <div class="price">￥{{item.price}}</div>
                                    </div>
                                    <div>
                                        <el-button size="small" class="btn" @click="handleAddCart(item)">加入购物车
                                        </el-button>
                                    </div>
                                </div>
                            </div>
                        </el-col>
                    </el-row>
                </div>
                <div class="search-more" @click="loadingMore()" v-if="collectList.length>9">点击加载更多</div>

                <!--        dialog-->
                <!--        删除提示弹窗-->
                <el-dialog :visible.sync="deleteDialogVisible" width="300px" center :modal="true" top="40vh">
                    <div align="center"><span>确定从收藏中移除选中的商品？</span></div>
                    <span slot="footer" class="dialog-footer">
                        <el-button type="danger" @click="deleteByIds()">确 定</el-button>
                        <el-button @click="deleteDialogVisible = false">取 消</el-button>
                    </span>
                </el-dialog>
            </el-main>
        </div>
    </div>
</template>

<script>
    import TopNav from "@c/TopNav/index.vue"
    import {
        deleteCollectByIds,
        getCollectList,
        addCart
    } from "../../api/index.js"

    export default {
        components: {
            TopNav
        },
        data() {
            return {
                imgUrl: process.env.VUE_APP_WAREHOUSE_API,
                showDelete: false, //显示批量删除
                chooseAll: false,
                radio: false,
                collectList: [], //收藏列表数据
                deleteData: [], //待删除的ids数据
                deleteDialogVisible: false,
                page: 1,
                size: 10,
                src: require('../../assets/test02.jpg')
            }
        },
        created() {
            this.searchList()
        },
        methods: {
            //全选按钮change
            chooseAllChange() {
                this.radio = this.chooseAll
            },
            //查询收藏列表
            searchList() {
                let param = {
                    pageNum: this.page,
                    pageSize: this.size
                }
                getCollectList(param).then(res => {
                    if (res.data.code === 200) {
                        let list = res.data.result.list
                        if (list.length == 0) {
                            this.$message({
                                message: "已全部加载",
                                type: 'info'
                            })
                        }
                        this.collectList = this.collectList.concat(list)
                    } else {
                        this.$message({
                            message: res.data.msg,
                            type: 'warning'
                        })
                    }
                }).catch(() => {
                    // this.$message.error("获取列表失败！")
                })
            },
            //删除单个
            deleteOne(item) {
                this.deleteData = []
                this.deleteData.push(item.id)
                this.deleteDialogVisible = true
            },
            //删除多个
            deleteMore() {
                this.deleteData = []
                //将列表中选中的放到待删数组中
                this.collectList.forEach(item => {
                    if (item.radio) {
                        this.deleteData.push(item.id)
                    }
                })
                this.deleteDialogVisible = true
            },
            //删除收藏
            deleteByIds() {
                this.deleteDialogVisible = false
                if (this.deleteData.length == 0) {
                    Message.error("至少选择一个...")
                    return
                }
                //整理ids
                let ids = ''
                this.deleteData.forEach(id => {
                    ids += ids == '' ? id : ',' + id
                })
                const loading = this.$loading({
                    lock: true,
                    text: '删除中...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                })
                let param = {
                    ids: ids
                }
                deleteCollectByIds(param).then(res => {
                    loading.close()
                    if (res.data.code === 200) {
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        })
                        //删除后从新查询
                        this.size = this.collectList.length
                        this.page = 1
                        this.collectList = []
                        this.searchList()
                    } else {
                        this.$message({
                            message: res.data.msg,
                            type: 'warning'
                        })
                    }
                }).catch(() => {
                    loading.close()
                    // this.$message.error("删除失败！")
                })
            },

            //加载更多
            loadingMore() {
                this.page++
                this.searchList()
            },

            //加入购物车
            handleAddCart(row) {
                const loading = this.$loading({
                    lock: true,
                    text: "正在加入购物车...",
                    spinner: "el-icon-loading",
                    background: "rgba(0, 0, 0, 0.7)"
                })
                addCart({
                    goodId: row.commodityId,
                    projectId: row.projectId,
                    payCount: 1,
                    serviceType: row.beOverdue,
                    goodsType: 1
                }).then(res => {
                    if (res.data.code === 200) {
                        this.$message({
                            message: '添加成功',
                            type: 'success'
                        })
                    } else {
                        this.$message({
                            message: res.data.msg,
                            type: 'warning'
                        })
                    }
                    loading.close()
                }).catch(err => {
                    loading.close()
                    this.$message.error('加入失败')
                })
            },
        }
    }
</script>

<style lang="scss" scoped>
    .el-body-box {
        width: 100%;
        background-color: white;

        .elMain {
            box-sizing: border-box;
            padding-right: 0;
            padding-left: 0;
            width: 1300px;
            margin: 0 auto;
            min-height: calc(100vh - 50px);

            .amount {
                color: #2196F3;
                margin: 0px 10px;
            }

            .manager-more {
                color: #2196F3;
                font-size: 14px;
                margin-left: 20px;
                cursor: pointer;
            }

            .commodity {
                border: 1px solid #BFBFBF;
                border-radius: 8px;
                width: 100%;
                height: 280px;
                position: relative;

                .img {
                    border-top-left-radius: 8px;
                    border-top-right-radius: 8px;
                    width: 100%;
                    height: 200px;
                }

                .icon-position {
                    position: relative;
                    color: #FFFFFF;
                    cursor: pointer;

                }

                .info {
                    position: relative;
                    bottom: 10px;
                    padding: 0px 10px;
                    display: flex;
                    justify-content: space-between;

                    .name {
                        max-width: 150px;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                        color: #666666;
                        font-size: 18px;
                    }

                    .price {
                        color: #FD8C13;
                        font-size: 16px;
                        font-weight: bold;
                    }

                    .btn {
                        color: #1DABEC;
                        font-size: 14px;
                        border: 1px solid #1DABEC;
                    }
                }
            }

            .search-more {
                text-align: center;
                font-size: 16px;
                color: #2196F3;
                margin: 20px 0px;
                cursor: pointer;
            }
        }
    }
</style>
